% This m-script estimates the reduced-form coefficients for the case with
% unobserved networks in LQT(2018)
% Date: April 29, 2019
function [Mu_k , Mu_0] = get_reduced_form(y,X);
% 'y' is n-by-L; and 'X' is n-by-k-by-L
n   = size(X,1); k = size(X,2); L = size(X,3);
dX  = X - repmat(mean(X,3),[1 1 L]); % demean 'X'
dy  = y - repmat(mean(y,2),1,L); % demean 'y'
Mu_k = zeros(n,n,k); Mu_0 = zeros(n,1);
% run regression for each 'i' and 'j'        
for i = 1:n, % loop in indivdiaul index 
    for j = 1:n, % loop in regressors
        mu_t = regress(dy(i,:)',squeeze(dX(j,:,:))'); 
        Mu_k(i,j,:) = mu_t; % slope coefficients (marginal effect of j's characteristics on i's outcome)
    end;
    % intercept in regression of 'i'
    Mu_0(i) = mean(y(i,:)) - sum(sum(mean(X,3).*squeeze(Mu_k(i,:,:)),2));    
end;
% mu_0 = mean(Mu_0); % avg. estimates for over-identified intercept 
                  